package com.hyjx.framework.service;

import com.hyjx.framework.common.Constants;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Random;

/**
 * 系统服务
 *
 * @author wangjd
 */
public class SystemService {
    private static Random radom = null;
    private static HttpServletRequest httpservletrequest;
    
    static {
        radom = new Random(10);
    }
    
    public static void setRequest(HttpServletRequest httpservletrequest) {
        SystemService.httpservletrequest = httpservletrequest;
    }
    
    /**
     * 获取request对象
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        if (RequestContextHolder.getRequestAttributes() != null) {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            return request;
        } else {
            return httpservletrequest;
        }
        
    }
    
    /**
     * 获取Response对象
     *
     * @return
     */
    public static HttpServletResponse getResponse() {
        return null;
    }
    
    /**
     * 获取基础路径
     *
     * @return
     */
    public static String getBasePath() {
        HttpServletRequest request = SystemService.getRequest();
        String path = request.getContextPath();
        return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    }
    
    /**
     * 生成随机数
     *
     * @return 999999以内的随机数
     */
    public static String getRadom() {
        return String.valueOf(radom.nextInt(999999));
    }
    
    /**
     * 设置系统初始化状态
     */
    public static void setSysInitStatus(String status) {
        CacheService.set("sys.init", status);
    }
    
    /**
     * 取系统初始化状态
     */
    public static String getSysInitStatus() {
        return (String) (CacheService.get("sys.init"));
    }
    
    /**
     * 取当前JSessionId
     *
     * @return
     */
    public static String getJSessionId() {
        HttpServletRequest req = SystemService.getRequest();
        Cookie[] cookies = req.getCookies();
        
        String userId = "";
        for (int i = 0; i < cookies.length; i++) {
            Cookie cook = cookies[i];
            if (Constants.JSESSIONID.equalsIgnoreCase(cook.getName()) || Constants.JSESSIONID_WEBLOGIC.equalsIgnoreCase(cook.getName())) { //获取键
                userId = cook.getValue().toString();
                break;
            }
        }
        return userId;
    }
    
    /**
     * 取当前主题
     *
     * @return
     */
    public static String getCurrentTheme(HttpServletRequest request) {
        String theme = "";
        if (request != null) {
            Cookie cookies[] = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals("theme")) {
                        theme = cookie.getValue();
                        break;
                    }
                }
            }
        }
        
        if (theme == null || theme.trim().length() == 0) {
            theme = ConfigManager.getConfig("ui.defaulttheme");
            if (theme == null || theme.trim().length() == 0) {
                if ("1".equals(ConfigManager.getUiLayout())) {
                    theme = "default";
                } else if ("2".equals(ConfigManager.getUiLayout())) {
                    theme = "layout-green";
                }
            }
        }
        
        if ("2".equals(ConfigManager.getUiLayout()) && !theme.contains("layout")) {
            theme = "layout-green";
        }
        if ("1".equals(ConfigManager.getUiLayout()) && theme.contains("layout")) {
            theme = "default";
        }
        return theme;
    }
    
    public static String getCurrentTheme() {
        String theme = getCurrentTheme(getRequest());
        return theme.replaceAll("layout-", "");
    }
    
}